import { screen } from 'electron'

export class CustomMove {
  win = null;
  isMoving = false;
  startPos = {
    winx: 0, 
    winy: 0,
    cx: 0,
    cy: 0,
  }

  constructor() {
  }

  init(win) {
    this.win = win;
  }

  moveStart() {
    const { x, y } = this.win.getBounds();
    const cursorpos = screen.getCursorScreenPoint();
    this.startPos = {
      winx: x, 
      winy: y,
      cx: cursorpos.x,
      cy: cursorpos.y,
    };

    this.isMoving = true;
    this.move();
  }

  move() {
    if (!this.isMoving) return;
     const cursorpos = screen.getCursorScreenPoint();
     const { winx, winy, cx, cy } = this.startPos;

     const x =  winx + (cursorpos.x - cx);
     const y =  winy + (cursorpos.y - cy);
     const { width, height } = this.win.getBounds();

     console.log(x, y);

     this.win.setBounds({
      x,
      y,
      width,
      height,
     })
     
     setTimeout(() => {
      this.move();
     }, 20);
  }

  end() {
    this.isMoving = false;
  }
}
